package com.ychen.oms.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ychen.api.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author chenyuliao
 * @version 1.0
 * @description
 * @date 2020/12/12 22:16
 */
@Controller
public class HelloController {

//    @Autowired
//    DemoService demoService;

    @DubboReference
    private DemoService demoService;

    @ResponseBody
    @RequestMapping("/hello")
    public String sayHello() {
        String res = demoService.sayHello("chenyuliao");
        System.out.println(res);
        return res;
    }

    @ResponseBody
    @RequestMapping("/getMap")
    public Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        System.out.println("map : hello ~ I am tomcat");
        map.put("aa", "hello ~ I am tomcat");
        return map;
    }

    @ResponseBody
    @RequestMapping("/getCourseList")
    public Map<String, String> getCourseList() {
        ExecutorService executorService = Executors.newFixedThreadPool(200);
        List<Future<?>> futureList = Lists.newArrayList();
        for (int i = 0; i < 10_0000; i++) {
            Future<?> future = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("请求数据....");
                    demoService.getCourseList("32323");
                    System.out.println("请求结束....");
                }
            });
            futureList.add(future);
        }
        for (Future<?> future : futureList) {
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("——————————————————————————————》》》》  end  .... ");

        return Maps.newHashMap();
    }

}
